﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Quartz;

namespace Cron
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string expstr = this.textBox1.Text.ToString();
            bool result = this.isValidExpression(expstr);
            if (result)
            {
                this.pictureBox1.Image = Cron.Properties.Resources.sign_check;
            }
            else
            {
                this.pictureBox1.Image = Cron.Properties.Resources.sign_error;
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            this.pictureBox1.Image = Cron.Properties.Resources.sign_info;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string expstr = this.textBox1.Text.ToString();
            bool result = this.isValidExpression(expstr);
            if (result)
            {
                this.pictureBox1.Image = Cron.Properties.Resources.sign_check;
                CronExpression exp = new CronExpression(expstr);
                DateTime dd = DateTime.Now;
                DateTimeOffset ddo = DateTime.SpecifyKind(dd, DateTimeKind.Local);
                for (int i = 0; i < 10; i++)
                {
                    ddo = (DateTimeOffset)exp.GetNextValidTimeAfter(ddo);
                    this.listBox1.Items.Add(ddo.LocalDateTime.ToString("yyyy-MM-dd HH:mm:ss"));
                }
                this.textBox2.Text = dd.ToString("yyyy-MM-dd HH:mm:ss");
            }
            else
            {
                this.pictureBox1.Image = Cron.Properties.Resources.sign_error;
            }
        }

        private bool isValidExpression(string expstr)
        {
            string[] expArr = expstr.Split(new char[1]{' '});
            if (expArr.Length == 6 || expArr.Length == 7)
            {
                try
                {
                    if (expArr.Length == 7)
                    {
                        int.Parse(expArr[6]);
                    }
                    bool isValid = Quartz.CronExpression.IsValidExpression(expstr);
                    if (isValid)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
            return false;
        }
    }
}
